<?php

//We could use CodeIgnitor

//ORM is an interface that must be inherited from; you may not instantiate this class
class ORM {
	protected $records = array();
	
	protected function __construct(){
		
	}
	
	protected function db_connect(&$db_conn, &$db_host, &$db_user, &$db_pass, &$db_name){
		if($db_conn)
			return;
		$db_conn = mysql_connect($db_host, $db_user, $db_pass);
		if(!$db_conn)
			throw new Exception("Unable to connect to database " . $db_name . " on " . $db_host);
		mysql_select_db($db_name, $db_conn);
		self::assertMySQLError($db_conn);
	}
	
	

	//protected function select($db_conn, $tableName /*...*/){
	//	mysql_query("SELECT * FROM `$tableName` WHERE ");
	//	#if(!self::$db_conn)
	//	#	$db_conn
	//}
	protected function rawSelectWhere($db_conn, $tableName, $clause){
		$sql = "SELECT * FROM `$tableName` WHERE $clause";
		$result = mysql_query($sql, $db_conn);
		self::assertMySQLError($db_conn);
		return $result;
	}
	protected function rawSelectBy($db_conn, $tableName, array $cols){
		$conds = array();
		foreach($cols as $key => $val)
			$conds[] = "`$key` = " . self::escapeValue($val);
		return self::rawSelectWhere($db_conn, $tableName, join(' AND ', $conds));
	}
	
	
	//Static helper functions
	public static function escapeValue($value){
		if(is_numeric($value))
			return $value;
		if(is_string($value))
			return '"' . mysql_escape_string($value) . '"';
		if(is_null($value))
			return 'NULL';
		if(is_bool($value))
			return $value ? '1' : '0';
		if(is_array($value))
			throw new Exception('Cannot convert arrays to single SQL values');
		if(is_object($value)){
			if(get_class($value) == 'DateTime')
				return self::escapeValue($value->format('c'));
			
			throw new Exception('Unable to convert ' . get_class($value) . ' object');
		}
		
	}
	public static function escapeValueList(array $values){
		$items = array();
		foreach($values as $value){
			$items[] = self::escapeValue($value);
		}
		return '(' . join(',', $items) . ')';
	}
	protected static function assertMySQLError(&$link = null){
		if(mysql_errno($link))
			throw new Exception(mysql_error($link), mysql_errno($link));
	}
};



class OpenScriptures extends ORM {
	static protected $db_host = 'localhost';
	static protected $db_name = 'open_scriptures';
	static private   $db_user = 'root';
	static private   $db_pass = '';
	static protected $db_conn = null;
	static protected $db_schema = array ( 'edition' => array ( 'columns' => array ( 'id' => array ( 'constraints' => array ( 'PRIMARY' => array ( 'CONSTRAINT_TYPE' => 'PRIMARY KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'PRI', ), 'work' => array ( 'constraints' => array ( 'fk_edition_work_id' => array ( 'REFERENCED_TABLE_NAME' => 'work', 'REFERENCED_COLUMN_NAME' => 'id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'smallint', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', ), 'type' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'enum', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 10, 'COLUMN_DEFAULT' => NULL, ), 'abbr' => array ( 'constraints' => array ( 'fk_edition_abbr_text_id' => array ( 'REFERENCED_TABLE_NAME' => 'text', 'REFERENCED_COLUMN_NAME' => 'text_id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => true, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', 'COLUMN_COMMENT' => 'NA26, MT, TR, etc.', ), 'title' => array ( 'constraints' => array ( 'fk_edition_title_text_id' => array ( 'REFERENCED_TABLE_NAME' => 'text', 'REFERENCED_COLUMN_NAME' => 'text_id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', 'COLUMN_COMMENT' => 'Nestle-Aland 27th Ed., Majority Text, Textus Receptus, etc.', ), 'date' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'datetime', 'IS_NULLABLE' => true, 'COLUMN_DEFAULT' => NULL, 'COLUMN_COMMENT' => 'The date that the earliest-used manuscript appeared', ), 'language' => array ( 'constraints' => array ( 'fk_edition_language_code' => array ( 'REFERENCED_TABLE_NAME' => 'language', 'REFERENCED_COLUMN_NAME' => 'code', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'varchar', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 6, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', ), 'ref_system' => array ( 'constraints' => array ( 'fk_edition_ref_system_id' => array ( 'REFERENCED_TABLE_NAME' => 'ref_system', 'REFERENCED_COLUMN_NAME' => 'id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'smallint', 'IS_NULLABLE' => true, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', 'COLUMN_COMMENT' => 'Default reference system used by the manuscript', ), 'copyright' => array ( 'constraints' => array ( 'fk_edition_copyright_text_id' => array ( 'REFERENCED_TABLE_NAME' => 'text', 'REFERENCED_COLUMN_NAME' => 'text_id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', ), ), ), 'edition_token' => array ( 'columns' => array ( 'id' => array ( 'constraints' => array ( 'PRIMARY' => array ( 'CONSTRAINT_TYPE' => 'PRIMARY KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'PRI', ), 'edition' => array ( 'constraints' => array ( 'fk_edition_token_edition_id' => array ( 'REFERENCED_TABLE_NAME' => 'edition', 'REFERENCED_COLUMN_NAME' => 'id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', ), 'token' => array ( 'constraints' => array ( 'fk_edition_token_token_id' => array ( 'REFERENCED_TABLE_NAME' => 'token', 'REFERENCED_COLUMN_NAME' => 'id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', ), 'precedence' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'tinyint', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => '1', 'COLUMN_COMMENT' => 'Value of 1 means that the token is included in the edition as a first-rate token; higher values denote less and less certain readings', ), 'traditional' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'enum', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 1, 'COLUMN_DEFAULT' => 'N', 'COLUMN_COMMENT' => 'Editions may indicate certain passages are traditional; if the passage is included, then precedence=1; otherwise precedence is greater than 1; coordinate traditional tokens should all have the same precedence level', ), 'alternate' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'enum', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 1, 'COLUMN_DEFAULT' => 'N', 'COLUMN_COMMENT' => 'If this is Y, then the precedence is assumed to be 2 or above; coordinate alternate words should all have the same precedence level', ), ), ), 'group' => array ( 'columns' => array ( 'id' => array ( 'constraints' => array ( 'PRIMARY' => array ( 'CONSTRAINT_TYPE' => 'PRIMARY KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'PRI', ), 'type' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'enum', 'IS_NULLABLE' => true, 'CHARACTER_MAXIMUM_LENGTH' => 14, 'COLUMN_DEFAULT' => NULL, ), 'title' => array ( 'constraints' => array ( 'fk_group_title_text_id' => array ( 'REFERENCED_TABLE_NAME' => 'text', 'REFERENCED_COLUMN_NAME' => 'text_id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', ), 'body' => array ( 'constraints' => array ( 'fk_group_body_text_id' => array ( 'REFERENCED_TABLE_NAME' => 'text', 'REFERENCED_COLUMN_NAME' => 'text_id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', ), 'owner' => array ( 'constraints' => array ( 'fk_group_creator_user_id' => array ( 'REFERENCED_TABLE_NAME' => 'user', 'REFERENCED_COLUMN_NAME' => 'id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', 'COLUMN_COMMENT' => 'The user that currently owns this group.', ), 'visibility' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'enum', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 7, 'COLUMN_DEFAULT' => 'public', 'COLUMN_COMMENT' => 'Whether or not group can be publicly seen.', ), 'public_role' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'enum', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 11, 'COLUMN_DEFAULT' => 'suggestor', ), ), ), 'group_token' => array ( 'columns' => array ( 'id' => array ( 'constraints' => array ( 'PRIMARY' => array ( 'CONSTRAINT_TYPE' => 'PRIMARY KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'PRI', ), 'group' => array ( 'constraints' => array ( 'fk_group_token_group_id' => array ( 'REFERENCED_TABLE_NAME' => 'group', 'REFERENCED_COLUMN_NAME' => 'id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', ), 'token' => array ( 'constraints' => array ( 'fk_group_token_token_id' => array ( 'REFERENCED_TABLE_NAME' => 'token', 'REFERENCED_COLUMN_NAME' => 'id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', ), 'subgroup' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'tinyint', 'IS_NULLABLE' => true, 'COLUMN_DEFAULT' => '1', 'COLUMN_COMMENT' => 'Number which commonly identifies which subgroup a token belongs in; if 1, then the token is in the primary group, if 2 then in the secondary group; the group_type determines what these subgroup numbers mean; if the group type is \'citation\', then tokens in', ), 'contributor' => array ( 'constraints' => array ( 'fk_group_token_contributor_id' => array ( 'REFERENCED_TABLE_NAME' => 'user', 'REFERENCED_COLUMN_NAME' => 'id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => true, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', 'COLUMN_COMMENT' => 'The user that added this version token to the group.', ), 'status' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'enum', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 7, 'COLUMN_DEFAULT' => 'added', 'COLUMN_COMMENT' => 'Multiple users may insert group_tokens, and if they set this to removed, then it will override the previous user\'s added token; this enabled history', ), 'suggested' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'enum', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 1, 'COLUMN_DEFAULT' => 'Y', ), 'timestamp' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'timestamp', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => 'CURRENT_TIMESTAMP', ), ), ), 'group_token_rating' => array ( 'columns' => array ( 'id' => array ( 'constraints' => array ( 'PRIMARY' => array ( 'CONSTRAINT_TYPE' => 'PRIMARY KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'PRI', ), 'group_token' => array ( 'constraints' => array ( 'fk_group_token_rating_group_token_id' => array ( 'REFERENCED_TABLE_NAME' => 'group_token', 'REFERENCED_COLUMN_NAME' => 'id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', ), 'contributor' => array ( 'constraints' => array ( 'fk_group_token_rating_contributor_id' => array ( 'REFERENCED_TABLE_NAME' => 'user', 'REFERENCED_COLUMN_NAME' => 'id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', ), 'rating' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'enum', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 1, 'COLUMN_DEFAULT' => NULL, ), ), ), 'group_user' => array ( 'columns' => array ( 'id' => array ( 'constraints' => array ( 'PRIMARY' => array ( 'CONSTRAINT_TYPE' => 'PRIMARY KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'PRI', ), 'group' => array ( 'constraints' => array ( 'fk_group_user_group_id' => array ( 'REFERENCED_TABLE_NAME' => 'group', 'REFERENCED_COLUMN_NAME' => 'id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', ), 'user' => array ( 'constraints' => array ( 'fk_group_user_user_id' => array ( 'REFERENCED_TABLE_NAME' => 'user', 'REFERENCED_COLUMN_NAME' => 'id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', ), 'role' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'enum', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 13, 'COLUMN_DEFAULT' => 'suggestor', ), ), ), 'language' => array ( 'columns' => array ( 'code' => array ( 'constraints' => array ( 'PRIMARY' => array ( 'CONSTRAINT_TYPE' => 'PRIMARY KEY', ), ), 'DATA_TYPE' => 'varchar', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 6, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'PRI', 'COLUMN_COMMENT' => 'ISO 639 language code', ), 'name:language' => array ( 'constraints' => array ( 'fk_text_name_text_id' => array ( 'REFERENCED_TABLE_NAME' => 'text', 'REFERENCED_COLUMN_NAME' => 'text_id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', 'COLUMN_COMMENT' => 'i18n language name', ), 'direction' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'enum', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 3, 'COLUMN_DEFAULT' => 'ltr', ), ), ), 'lexeme' => array ( 'columns' => array ( 'id' => array ( 'constraints' => array ( 'PRIMARY' => array ( 'CONSTRAINT_TYPE' => 'PRIMARY KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'PRI', ), 'lemma' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'varchar', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 255, 'COLUMN_DEFAULT' => NULL, 'COLUMN_COMMENT' => 'ISO 639 language code', ), 'language' => array ( 'constraints' => array ( 'fk_lexeme_language_code' => array ( 'REFERENCED_TABLE_NAME' => 'language', 'REFERENCED_COLUMN_NAME' => 'code', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'varchar', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 6, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', ), 'strongs_number' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'varchar', 'IS_NULLABLE' => true, 'CHARACTER_MAXIMUM_LENGTH' => 16, 'COLUMN_DEFAULT' => NULL, ), ), ), 'ref' => array ( 'columns' => array ( 'id' => array ( 'constraints' => array ( 'PRIMARY' => array ( 'CONSTRAINT_TYPE' => 'PRIMARY KEY', ), ), 'DATA_TYPE' => 'smallint', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'PRI', ), 'ref_system' => array ( 'constraints' => array ( 'fk_ref_ref_system_id' => array ( 'REFERENCED_TABLE_NAME' => 'ref_system', 'REFERENCED_COLUMN_NAME' => 'id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'smallint', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', ), 'type' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'enum', 'IS_NULLABLE' => true, 'CHARACTER_MAXIMUM_LENGTH' => 12, 'COLUMN_DEFAULT' => NULL, ), 'title' => array ( 'constraints' => array ( 'fk_ref_title_text_id' => array ( 'REFERENCED_TABLE_NAME' => 'text', 'REFERENCED_COLUMN_NAME' => 'text_id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', 'COLUMN_COMMENT' => 'Psalms, Matthew, Parable of the Sower, etc.', ), 'parent:child' => array ( 'constraints' => array ( 'fk_ref_parent_ref_id' => array ( 'REFERENCED_TABLE_NAME' => 'ref', 'REFERENCED_COLUMN_NAME' => 'id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'smallint', 'IS_NULLABLE' => true, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', 'COLUMN_COMMENT' => 'The ref that contains this one, i.e. if this ref is a verse, this points to the containing chapter', ), 'numerical_start' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'smallint', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_COMMENT' => 'Number which represents the referant\'s start, as in if the ref_type is book, the numerical start would be the sort index', ), 'numerical_end' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'smallint', 'IS_NULLABLE' => true, 'COLUMN_DEFAULT' => NULL, 'COLUMN_COMMENT' => 'End of the start, as in a section or verse range', ), 'start_token' => array ( 'constraints' => array ( 'fk_ref_start_token_id' => array ( 'REFERENCED_TABLE_NAME' => 'token', 'REFERENCED_COLUMN_NAME' => 'id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', 'COLUMN_COMMENT' => 'The token that begins the reference', ), 'end_token' => array ( 'constraints' => array ( 'fk_ref_end_token_id' => array ( 'REFERENCED_TABLE_NAME' => 'token', 'REFERENCED_COLUMN_NAME' => 'id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', ), ), ), 'ref_system' => array ( 'columns' => array ( 'id' => array ( 'constraints' => array ( 'PRIMARY' => array ( 'CONSTRAINT_TYPE' => 'PRIMARY KEY', ), ), 'DATA_TYPE' => 'smallint', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'PRI', ), 'work' => array ( 'constraints' => array ( 'fk_ref_system_work_id' => array ( 'REFERENCED_TABLE_NAME' => 'work', 'REFERENCED_COLUMN_NAME' => 'id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'smallint', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', 'COLUMN_COMMENT' => 'Name of the precedence level', ), 'abbr' => array ( 'constraints' => array ( 'fk_ref_system_abbr_text_id' => array ( 'REFERENCED_TABLE_NAME' => 'text', 'REFERENCED_COLUMN_NAME' => 'text_id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => true, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', 'COLUMN_COMMENT' => 'KJV, NRSV, etc.', ), 'title' => array ( 'constraints' => array ( 'fk_ref_system_title_text_id' => array ( 'REFERENCED_TABLE_NAME' => 'text', 'REFERENCED_COLUMN_NAME' => 'text_id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', 'COLUMN_COMMENT' => 'King James Version, etc.', ), ), ), 'text' => array ( 'columns' => array ( 'text_id' => array ( 'constraints' => array ( 'PRIMARY' => array ( 'CONSTRAINT_TYPE' => 'PRIMARY KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'PRI', ), 'language:name' => array ( 'constraints' => array ( 'PRIMARY' => array ( 'CONSTRAINT_TYPE' => 'PRIMARY KEY', ), 'fk_text_language_code' => array ( 'REFERENCED_TABLE_NAME' => 'language', 'REFERENCED_COLUMN_NAME' => 'code', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'varchar', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 6, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'PRI', ), 'text' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'text', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 65535, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', ), 'domain' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'enum', 'IS_NULLABLE' => true, 'CHARACTER_MAXIMUM_LENGTH' => 10, 'COLUMN_DEFAULT' => NULL, 'COLUMN_COMMENT' => 'When null, the domain is for the system; otherwise, it is for the corresponding table.', ), 'source:translation' => array ( 'constraints' => array ( 'fk_text_source_text_id' => array ( 'REFERENCED_TABLE_NAME' => 'text', 'REFERENCED_COLUMN_NAME' => 'text_id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => true, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', 'COLUMN_COMMENT' => 'The other entry which is the source of this text', ), ), ), 'token' => array ( 'columns' => array ( 'id' => array ( 'constraints' => array ( 'PRIMARY' => array ( 'CONSTRAINT_TYPE' => 'PRIMARY KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'PRI', ), 'token' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'varchar', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 255, 'COLUMN_DEFAULT' => NULL, ), 'type' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'enum', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 11, 'COLUMN_DEFAULT' => 'word', ), 'position' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, ), ), ), 'token_parsing' => array ( 'columns' => array ( 'id' => array ( 'constraints' => array ( 'PRIMARY' => array ( 'CONSTRAINT_TYPE' => 'PRIMARY KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'PRI', ), 'token' => array ( 'constraints' => array ( 'fk_token_parsing_token_id' => array ( 'REFERENCED_TABLE_NAME' => 'token', 'REFERENCED_COLUMN_NAME' => 'id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', ), 'lexeme' => array ( 'constraints' => array ( 'fk_token_parsing_lexeme_id' => array ( 'REFERENCED_TABLE_NAME' => 'lexeme', 'REFERENCED_COLUMN_NAME' => 'id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', 'COLUMN_COMMENT' => 'The base form of a word', ), 'morph' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'varchar', 'IS_NULLABLE' => true, 'CHARACTER_MAXIMUM_LENGTH' => 255, 'COLUMN_DEFAULT' => NULL, 'COLUMN_COMMENT' => 'Grammatical information for the token', ), 'pos' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'varchar', 'IS_NULLABLE' => true, 'CHARACTER_MAXIMUM_LENGTH' => 32, 'COLUMN_DEFAULT' => NULL, 'COLUMN_COMMENT' => 'The function of a word according to a particular view of the language\'s syntax', ), 'contributor' => array ( 'constraints' => array ( 'fk_token_parsing_contributor_id' => array ( 'REFERENCED_TABLE_NAME' => 'user', 'REFERENCED_COLUMN_NAME' => 'id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => true, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', 'COLUMN_COMMENT' => 'The user who provided the parsing.', ), ), ), 'user' => array ( 'columns' => array ( 'id' => array ( 'constraints' => array ( 'PRIMARY' => array ( 'CONSTRAINT_TYPE' => 'PRIMARY KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'PRI', ), 'name' => array ( 'constraints' => array ( 'fk_user_name_text_id' => array ( 'REFERENCED_TABLE_NAME' => 'text', 'REFERENCED_COLUMN_NAME' => 'text_id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', 'COLUMN_COMMENT' => 'John Smith, Fred Thompson, etc.', ), 'username' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'varchar', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 64, 'COLUMN_DEFAULT' => NULL, 'COLUMN_COMMENT' => 'johnsmith1, fthompson3, etc.', ), 'password' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'tinyblob', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 255, 'COLUMN_DEFAULT' => NULL, 'COLUMN_COMMENT' => 'AES-encrpyted password', ), 'register_date' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'datetime', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, ), 'preferred_language:preferring_user' => array ( 'constraints' => array ( 'fk_user_preferred_language_code' => array ( 'REFERENCED_TABLE_NAME' => 'language', 'REFERENCED_COLUMN_NAME' => 'code', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'varchar', 'IS_NULLABLE' => true, 'CHARACTER_MAXIMUM_LENGTH' => 6, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', 'COLUMN_COMMENT' => 'Their preferred language', ), 'role' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'enum', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 13, 'COLUMN_DEFAULT' => 'suggestor', ), ), ), 'user_language' => array ( 'columns' => array ( 'user' => array ( 'constraints' => array ( 'PRIMARY' => array ( 'CONSTRAINT_TYPE' => 'PRIMARY KEY', ), 'fk_user_language_user_id' => array ( 'REFERENCED_TABLE_NAME' => 'user', 'REFERENCED_COLUMN_NAME' => 'id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'PRI', ), 'language' => array ( 'constraints' => array ( 'PRIMARY' => array ( 'CONSTRAINT_TYPE' => 'PRIMARY KEY', ), 'fk_user_language_language_code' => array ( 'REFERENCED_TABLE_NAME' => 'language', 'REFERENCED_COLUMN_NAME' => 'code', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'varchar', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 6, 'COLUMN_DEFAULT' => '', 'COLUMN_KEY' => 'PRI', 'COLUMN_COMMENT' => 'Their preferred language', ), 'proficiency' => array ( 'constraints' => array ( ), 'DATA_TYPE' => 'enum', 'IS_NULLABLE' => false, 'CHARACTER_MAXIMUM_LENGTH' => 12, 'COLUMN_DEFAULT' => 'native', 'COLUMN_COMMENT' => 'ACTFL Proficiency Guidelines: http://www.sil.org/lingualinks/languagelearning/OtherResources/ACTFLProficiencyGuidelines/contents.htm', ), ), ), 'work' => array ( 'columns' => array ( 'id' => array ( 'constraints' => array ( 'PRIMARY' => array ( 'CONSTRAINT_TYPE' => 'PRIMARY KEY', ), ), 'DATA_TYPE' => 'smallint', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'PRI', ), 'abbr' => array ( 'constraints' => array ( 'fk_work_abbr_text_id' => array ( 'REFERENCED_TABLE_NAME' => 'text', 'REFERENCED_COLUMN_NAME' => 'text_id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => true, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', 'COLUMN_COMMENT' => 'OT, NT, etc.', ), 'title' => array ( 'constraints' => array ( 'fk_work_title_text_id' => array ( 'REFERENCED_TABLE_NAME' => 'text', 'REFERENCED_COLUMN_NAME' => 'text_id', 'CONSTRAINT_TYPE' => 'FOREIGN KEY', ), ), 'DATA_TYPE' => 'int', 'IS_NULLABLE' => false, 'COLUMN_DEFAULT' => NULL, 'COLUMN_KEY' => 'MUL', 'COLUMN_COMMENT' => 'Old Testament, New Testament, Qur\'an, etc', ), ), ), );
	
	function __construct(){
		parent::__construct();
		self::db_connect(self::$db_conn, self::$db_host, self::$db_user, self::$db_pass, self::$db_name);
	}
	
	function select($sql){
		self::db_connect(self::$db_conn, self::$db_host, self::$db_user, self::$db_pass, self::$db_name);
		
		echo __CLASS__;
		
		
		$result = mysql_query("SELECT * FROM user", self::$db_conn);
		$rows = array();
		while($row = mysql_fetch_object($result)){
			$rows[] = $row;
		}
		return $rows;
	}
};


//Now list all of the tables

class User extends OpenScriptures {
	static protected $table_name = "user";
	
	
};

echo '<pre>';

$s = new user();
print_r($s->select("all"));




?>